/**
 * 产品价格表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzucai
 * @since 2019/11/26
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate','jquery'], function (exports) {
    var $ = layui.$;
    var table = layui.table;
    var laypage = layui.laypage;
    var laydate = layui.laydate;
    let layer = layui.layer;
    var form = layui.form;
    let index;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-230',
        done: function (res) {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
            res.data.forEach(function (item,index) {
                if(item.status == 1){
                    layui.$('#zmn-table').next('div').find(".layui-table-body tbody tr[data-index='"+index+"'] td[data-field='status']").css({'color': '#DCDCDC' });
                }
            });
        }
    });

    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    zmnProduct.laySelectSpecial(form, "/product/listChilds.action", "/product/listCateChilds.action", "servCategId", "categOneId", "categId");

    // 监听导出事件
    form.on('submit(query-export)', function (data) {
        let index = layer.load(1, {
            shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
        });

        zmnUtil.downloadLimitByConfig("/product/tariff/exportExcel.action?" + $("#product_base_price").serialize(), "产品价格表.xlsx", function () {
            layer.close(index);
        });
        return false;
    });

    var ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function () {
        ids = table.checkStatus('zmn-table').data.map((item) => item.relateId);
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        if(obj.field=='sort'){
            zmnPage.laySortPositiveInteger(obj.value,"/service/tariff/updateSort.action",obj.data.tariffId,100000,1000)
        }
    });

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event == 'CUSTOM_EXPORT') {
            $("#query-export").click();
            return ;
        }
        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少一条数据！");
        }
        zmnPage.layStartStop(obj, "/product/tariff/updateStatus.action", ids.join(","));
    });

    //对外暴露的接口
    exports('serv_product_tariff_list', {});
});

var zmnProduct = {
    /**
     * 多级下拉框联动
     * @param form layui.form
     * @param url1 selectOneId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param url1 selectTwoId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param selectOneId 第1级select的ID，不含#号
     * @param selectTwoId 第2级select的ID，不含#号
     * @param selectThrId 第3级select的ID，不含#号，如无第3级则两级联动
     */
    laySelectSpecial: function (form, url1, url2, selectOneId, selectTwoId, selectThrId) {
        let $ = layui.$;
        let servCategId;
        let categOneId;
        form.on("select(" + selectOneId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            servCategId = obj.value;
            $.ajax({
                type: "GET", url: url1,
                dataType: "json", data: {parentId: servCategId},
                success: function (items) {
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    form.render("select");
                }
            });
        });
        if (zmnUtil.isEmptyNull(selectThrId)) {
            return;
        }
        ;

        form.on("select(" + selectTwoId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            categOneId = obj.value;
            servCategId = $('#servCategId').val();;
            $.ajax({
                type: "GET", url: url2,
                dataType: "json", data: {categOneId: obj.value, servCategId: servCategId},
                success: function (items) {
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectThrId).html(optionHtml);
                    form.render("select");
                }
            });
        });
    },
}